<?php
/**
 * TomatoCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version 	$Id: Employee.php 5341 2010-09-07 09:04:47Z huuphuoc $
 * @since		2.0.5
 */

class Employee_Models_Dao_Pdo_Mysql_Employee extends Tomato_Model_Dao
	implements Employee_Models_Interface_Employee
{
	public function convert($entity) 
	{
		return new Employee_Models_Employee($entity); 
	}
	// Tìm mã số tiếp theo của employee thêm mới vào
	public function getNextId(){		
		$config 					= Tomato_Config::getConfig(); 
		$prefix_code_employee		= $config->get('prefix_code')->employee_code;
		$prefix_code_employee_length = strlen($prefix_code_employee);
		$name_ini_length			= 'employee_code_length';
		$length_max_id				= $config->get('prefix_code')->$name_ini_length;
			
		$select = $this->_conn
					->select()
					->from(array('p' => $this->_prefix . 'employee'),'MAX(SUBSTR(p.id,'.($prefix_code_employee_length+1).')) as MAX_ID');
		$row = $select->query()
			   		  ->fetch();	
		$max_id =  (null == $row) ? 0 : $row->MAX_ID;
		$max_id++;
		$addition_zero_num 		= $length_max_id - strlen($max_id) - strlen($prefix_code_employee);
		
		$code = $prefix_code_employee;
		for($i=1;$i<=$addition_zero_num;$i++)
			$code.='0';
		$code.= $max_id;
		return $code;	
	}
	
	public function find($offset,$count,$exp=null){
		$select = $this->_conn
					->select()
					->from(array('e' => $this->_prefix . 'employee'));
		if ($exp) {
            if (isset($exp['shop_id'])) {
				$select->where("e.shop_id=?",$exp['shop_id']);
			}			
            if (isset($exp['employee_id'])) {
				$select->where("e.employee_id=?",$exp['employee_id']);
			}
            if (isset($exp['deleted'])) {
				$select->where("e.deleted =?",$exp['deleted']);
			}
		}		
        $select->order('e.id ASC');
        if(null != $count){
            $select->limit($count, $offset);
        }
        $rs = $select->query()
                     ->fetchAll();
        return new Tomato_Model_RecordSet($rs, $this);		
	}
	
	
	public 	function count($exp){
		$select = $this->_conn
                        ->select()
                        ->from(array('e' => $this->_prefix . 'employee'), array('num_employee' => 'COUNT(*)'));
        if ($exp) {
            if (isset($exp['shop_id'])) {
				$select->where("e.shop_id=?",$exp['shop_id']);
			}			        	
            if (isset($exp['employee_id'])) {
				$select->where("e.employee_id=?",$exp['employee_id']);
			}
            if (isset($exp['deleted'])) {
				$select->where("e.deleted =?",$exp['deleted']);
			}			
        }
        return $select->query()->fetch()->num_employee;	
	}	
	
	public function getById($id) 
	{
		$row = $this->_conn
					->select()
					->from(array('e' => $this->_prefix . 'employee'))
					->where('e.id = ?', $id)
					->limit(1)
					->query()
					->fetch();
		return (null == $row) ? null : new Employee_Models_Employee($row);
	}

	public function add($employee) 
	{
			$data = array(
				"id"					=>$employee->id,	
				"core_user_id"			=>$employee->core_user_id,
				"shop_id"				=>$employee->shop_id,
				"name"					=>$employee->name,
				"sex"					=>$employee->sex,
				"birthday"				=>$employee->birthday,
				"identity_card_number"	=>$employee->identity_card_number,
				"description"			=>@$employee->description,
				"deleted"				=>@$employee->deleted,
			);
			
			if (isset($employee->id) && $employee->id != null) {
				$data['id'] = $employee->id;
				$this->_conn->insert($this->_prefix . 'employee', $data);
				return $employee->id;				
			} else {
				$this->_conn->insert($this->_prefix . 'employee', $data);
				return $this->_conn->lastInsertId($this->_prefix . 'employee');
			}
	}
	
	public function update($employee) 
	{
		$this->_conn->update($this->_prefix . 'employee', 
							array(
									"core_user_id"			=>$employee->core_user_id,
									"shop_id"				=>$employee->shop_id,
									"name"					=>$employee->name,
									"sex"					=>$employee->sex,
									"birthday"				=>$employee->birthday,
									"identity_card_number"	=>$employee->identity_card_number,
									"description"			=>$employee->description,
									"deleted"				=>@$employee->deleted,
							), 
							array(
								'id = ?' => $employee->id,
							));
	}
	
	public function delete($employee) 
	{
		$this->_conn->delete($this->_prefix . 'employee', 
							array(
								'id = ?' => $employee->id,
							));
	}
	public function getByCoreUserId($core_user_id){
		$row = $this->_conn
					->select()
					->from(array('e' => $this->_prefix . 'employee'))
					->where('e.core_user_id = ?', $core_user_id)
					->limit(1)
					->query()
					->fetch();
		return (null == $row) ? null : new Employee_Models_Employee($row);
	}	
}